<template>
  <div class="login">
    <div id="particles-js"></div>
    <div class="login-overlay"></div>
    <div class="particle particle1"></div>
    <div class="particle particle2"></div>
    <div class="particle particle3"></div>
    <div class="particle particle4"></div>
    <div class="particle particle5"></div>
    <div class="particle particle6"></div>
    <div class="particle particle7"></div>
    <div class="particle particle8"></div>
    <div class="light-beam beam1"></div>
    <div class="light-beam beam2"></div>
    <div class="light-beam beam3"></div>
    <div class="circuit-line horizontal line1"></div>
    <div class="circuit-line horizontal line2"></div>
    <div class="circuit-line horizontal line3"></div>
    <div class="circuit-line vertical line1"></div>
    <div class="circuit-line vertical line2"></div>
    <div class="circuit-line vertical line3"></div>
    <div class="tech-circle circle1"></div>
    <div class="tech-circle circle2"></div>
    <div class="tech-circle circle3"></div>
    <div class="tech-circle circle4"></div>
    <div class="data-stream stream1"></div>
    <div class="data-stream stream2"></div>
    <div class="data-stream stream3"></div>
    <div class="hologram-effect"></div>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="title">锐智速控管理系统</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon
            slot="prefix"
            icon-class="user"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaOnOff">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="validCode"
            class="el-input__icon input-icon"
          />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
      <el-checkbox
        v-model="loginForm.rememberMe"
        style="margin: 0px 0px 25px 0px"
        >记住密码</el-checkbox
      >
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right" v-if="register">
          <router-link class="link-type" :to="'/register'"
            >立即注册</router-link
          >
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span @click="toIPC">陕ICP备2022002135号-1</span>
    </div> -->
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import "particles.js";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  mounted() {
    this.initParticles();
    this.initGradientAnimation();
  },
  methods: {
    toIPC() {
      window.open("https://beian.miit.gov.cn/", "_blank");
    },
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
    initParticles() {
      window.particlesJS("particles-js", {
        particles: {
          number: {
            value: 200,
            density: {
              enable: true,
              value_area: 800,
            },
          },
          color: {
            value: ["#0066ff", "#0099ff", "#00ccff", "#3399ff"],
          },
          shape: {
            type: ["circle", "triangle"],
          },
          opacity: {
            value: 0.6,
            random: true,
            anim: {
              enable: true,
              speed: 1.5,
              opacity_min: 0.1,
              sync: false,
            },
          },
          size: {
            value: 3,
            random: true,
            anim: {
              enable: true,
              speed: 2,
              size_min: 0.1,
              sync: false,
            },
          },
          line_linked: {
            enable: true,
            distance: 150,
            color: "#0066ff",
            opacity: 0.4,
            width: 1.5,
          },
          move: {
            enable: true,
            speed: 2,
            direction: "none",
            random: true,
            straight: false,
            out_mode: "out",
            bounce: false,
            attract: {
              enable: true,
              rotateX: 600,
              rotateY: 1200,
            },
          },
        },
        interactivity: {
          detect_on: "canvas",
          events: {
            onhover: {
              enable: true,
              mode: ["grab", "bubble"],
            },
            onclick: {
              enable: true,
              mode: "push",
            },
            resize: true,
          },
          modes: {
            grab: {
              distance: 140,
              line_linked: {
                opacity: 0.8,
              },
            },
            bubble: {
              distance: 200,
              size: 6,
              duration: 2,
              opacity: 0.8,
              speed: 3,
            },
            push: {
              particles_nb: 4,
            },
          },
        },
        retina_detect: true,
      });
    },
    initGradientAnimation() {
      const overlay = document.querySelector(".login-overlay");
      let hue = 210; // 从蓝色开始

      setInterval(() => {
        hue = (hue + 0.5) % 360;
        overlay.style.background = `linear-gradient(135deg, 
          rgba(255, 255, 255, 0.95) 0%,
          rgba(240, 248, 255, 0.9) 25%,
          rgba(230, 245, 255, 0.85) 50%,
          rgba(220, 240, 255, 0.9) 75%,
          rgba(255, 255, 255, 0.95) 100%
        )`;
      }, 100);
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #ffffff 0%, #f8fbff 25%, #e6f3ff 50%, #d4ebff 75%, #ffffff 100%);
  background-image: 
    radial-gradient(circle at 20% 80%, rgba(0, 102, 255, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(0, 153, 255, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 40% 40%, rgba(0, 204, 255, 0.05) 0%, transparent 50%),
    linear-gradient(90deg, rgba(0, 102, 255, 0.03) 1px, transparent 1px),
    linear-gradient(0deg, rgba(0, 102, 255, 0.03) 1px, transparent 1px);
  background-size: 100% 100%, 100% 100%, 100% 100%, 30px 30px, 30px 30px;

  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(
      circle at 50% 50%,
      rgba(0, 102, 255, 0.08) 0%,
      transparent 70%
    );
    animation: pulse 6s ease-in-out infinite;
  }

  .particle {
    position: absolute;
    background: linear-gradient(45deg, #0066ff, #0099ff);
    border-radius: 50%;
    pointer-events: none;
    box-shadow: 0 0 10px rgba(0, 102, 255, 0.5);

    &.particle1 {
      width: 6px;
      height: 6px;
      top: 15%;
      left: 25%;
      animation: floatParticle 10s ease-in-out infinite;
    }

    &.particle2 {
      width: 4px;
      height: 4px;
      top: 35%;
      right: 20%;
      animation: floatParticle 8s ease-in-out infinite 1s;
    }

    &.particle3 {
      width: 7px;
      height: 7px;
      bottom: 25%;
      left: 35%;
      animation: floatParticle 12s ease-in-out infinite 2s;
    }

    &.particle4 {
      width: 3px;
      height: 3px;
      top: 55%;
      left: 15%;
      animation: floatParticle 9s ease-in-out infinite 0.5s;
    }

    &.particle5 {
      width: 5px;
      height: 5px;
      bottom: 35%;
      right: 30%;
      animation: floatParticle 11s ease-in-out infinite 1.5s;
    }

    &.particle6 {
      width: 4px;
      height: 4px;
      top: 70%;
      left: 60%;
      animation: floatParticle 7s ease-in-out infinite 0.8s;
    }

    &.particle7 {
      width: 6px;
      height: 6px;
      top: 25%;
      left: 70%;
      animation: floatParticle 13s ease-in-out infinite 2.5s;
    }

    &.particle8 {
      width: 3px;
      height: 3px;
      bottom: 20%;
      left: 80%;
      animation: floatParticle 9s ease-in-out infinite 1.2s;
    }
  }

  .light-beam {
    position: absolute;
    width: 100%;
    height: 3px;
    background: linear-gradient(
      90deg,
      transparent 0%,
      rgba(0, 102, 255, 0.6) 20%,
      rgba(0, 153, 255, 0.8) 50%,
      rgba(0, 102, 255, 0.6) 80%,
      transparent 100%
    );
    animation: beamMove 4s linear infinite;
    opacity: 0.7;
    box-shadow: 0 0 20px rgba(0, 102, 255, 0.3);

    &.beam1 {
      top: 15%;
    }
    &.beam2 {
      top: 50%;
    }
    &.beam3 {
      top: 85%;
    }
  }

  .circuit-line {
    position: absolute;
    background: linear-gradient(90deg, transparent, #0066ff, transparent);
    animation: circuitGlow 3s ease-in-out infinite;
    box-shadow: 0 0 10px rgba(0, 102, 255, 0.3);

    &.horizontal {
      height: 2px;
      width: 120px;
      &.line1 {
        top: 25%;
        left: 8%;
      }
      &.line2 {
        top: 65%;
        right: 12%;
      }
      &.line3 {
        bottom: 30%;
        left: 25%;
      }
    }

    &.vertical {
      width: 2px;
      height: 120px;
      &.line1 {
        top: 15%;
        left: 20%;
      }
      &.line2 {
        top: 45%;
        right: 25%;
      }
      &.line3 {
        bottom: 25%;
        left: 45%;
      }
    }
  }

  .tech-circle {
    position: absolute;
    border: 2px solid rgba(0, 102, 255, 0.3);
    border-radius: 50%;
    animation: circlePulse 4s ease-in-out infinite;
    background: radial-gradient(circle, rgba(0, 102, 255, 0.1) 0%, transparent 70%);

    &.circle1 {
      width: 250px;
      height: 250px;
      top: 5%;
      right: 5%;
    }

    &.circle2 {
      width: 180px;
      height: 180px;
      bottom: 10%;
      left: 10%;
    }

    &.circle3 {
      width: 120px;
      height: 120px;
      top: 45%;
      left: 45%;
      transform: translate(-50%, -50%);
    }

    &.circle4 {
      width: 80px;
      height: 80px;
      top: 75%;
      right: 20%;
    }
  }

  .data-stream {
    position: absolute;
    width: 2px;
    height: 100px;
    background: linear-gradient(180deg, transparent, #0066ff, transparent);
    animation: dataFlow 2s linear infinite;
    opacity: 0.6;

    &.stream1 {
      top: 10%;
      left: 10%;
      animation-delay: 0s;
    }

    &.stream2 {
      top: 20%;
      right: 15%;
      animation-delay: 0.7s;
    }

    &.stream3 {
      bottom: 15%;
      left: 30%;
      animation-delay: 1.4s;
    }
  }

  .hologram-effect {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: 
      repeating-linear-gradient(
        90deg,
        transparent,
        transparent 2px,
        rgba(0, 102, 255, 0.02) 2px,
        rgba(0, 102, 255, 0.02) 4px
      );
    animation: hologramScan 8s linear infinite;
    pointer-events: none;
  }
}

@keyframes floatParticle {
  0%, 100% {
    transform: translate(0, 0) scale(1) rotate(0deg);
    opacity: 0.6;
  }
  25% {
    transform: translate(30px, -30px) scale(1.3) rotate(90deg);
    opacity: 1;
  }
  50% {
    transform: translate(60px, 0) scale(1) rotate(180deg);
    opacity: 0.6;
  }
  75% {
    transform: translate(30px, 30px) scale(0.7) rotate(270deg);
    opacity: 0.3;
  }
}

@keyframes pulse {
  0%, 100% {
    opacity: 0.5;
    transform: scale(1);
  }
  50% {
    opacity: 0.8;
    transform: scale(1.05);
  }
}

@keyframes beamMove {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

@keyframes circuitGlow {
  0%, 100% {
    opacity: 0.2;
    box-shadow: 0 0 5px rgba(0, 102, 255, 0.2);
  }
  50% {
    opacity: 0.6;
    box-shadow: 0 0 15px rgba(0, 102, 255, 0.5);
  }
}

@keyframes circlePulse {
  0%, 100% {
    transform: scale(1);
    opacity: 0.3;
    border-color: rgba(0, 102, 255, 0.3);
  }
  50% {
    transform: scale(1.1);
    opacity: 0.6;
    border-color: rgba(0, 102, 255, 0.6);
  }
}

@keyframes dataFlow {
  0% {
    transform: translateY(-100px);
    opacity: 0;
  }
  50% {
    opacity: 0.8;
  }
  100% {
    transform: translateY(100px);
    opacity: 0;
  }
}

@keyframes hologramScan {
  0% {
    transform: translateY(-100%);
  }
  100% {
    transform: translateY(100%);
  }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #0066ff;
  font-family: "Microsoft YaHei", sans-serif;
  font-size: 36px;
  font-weight: bold;
  letter-spacing: 3px;
  text-shadow: 0 0 20px rgba(0, 102, 255, 0.3);
  background: linear-gradient(45deg, #0066ff, #0099ff, #00ccff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: titleGlow 3s ease-in-out infinite;
}

@keyframes titleGlow {
  0%, 100% {
    filter: drop-shadow(0 0 10px rgba(0, 102, 255, 0.3));
  }
  50% {
    filter: drop-shadow(0 0 20px rgba(0, 102, 255, 0.6));
  }
}

.login-form {
  border-radius: 15px;
  background: rgba(255, 255, 255, 0.95);
  width: 480px;
  padding: 40px 40px 20px 40px;
  box-shadow: 
    0 20px 40px rgba(0, 102, 255, 0.1),
    0 0 0 1px rgba(0, 102, 255, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(20px);
  z-index: 2;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  border: 1px solid rgba(0, 102, 255, 0.2);

  .el-form-item {
    width: 100%;
    margin-bottom: 25px;
  }

  .el-input {
    height: 45px;
    margin-bottom: 25px;
    position: relative;
    z-index: 2;
    width: 100%;
    max-width: 380px;

    input {
      height: 45px;
      border-radius: 8px;
      border: 2px solid rgba(0, 102, 255, 0.2);
      background: rgba(255, 255, 255, 0.9);
      color: #333;
      font-weight: 500;
      transition: all 0.3s ease;
      text-align: center;
      font-size: 16px;
      backdrop-filter: blur(10px);

      &::placeholder {
        color: rgba(0, 102, 255, 0.6);
        font-weight: 400;
      }

      &:focus {
        border-color: #0066ff;
        box-shadow: 0 0 20px rgba(0, 102, 255, 0.3);
        background: rgba(255, 255, 255, 1);
        animation: inputGlow 2s infinite;
      }
    }
  }

  .input-icon {
    height: 45px;
    width: 16px;
    margin-left: 2px;
    color: #0066ff;
  }

  .el-button {
    height: 45px;
    border-radius: 8px;
    background: linear-gradient(45deg, #0066ff, #0099ff);
    border: none;
    color: #ffffff;
    font-size: 16px;
    font-weight: bold;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
    width: 100%;
    max-width: 380px;
    box-shadow: 0 5px 15px rgba(0, 102, 255, 0.3);

    &::before {
      content: "";
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background: linear-gradient(
        90deg,
        transparent,
        rgba(255, 255, 255, 0.3),
        transparent
      );
      transition: 0.6s;
    }

    &:hover {
      background: linear-gradient(45deg, #0052cc, #0077cc);
      transform: translateY(-3px);
      box-shadow: 0 10px 25px rgba(0, 102, 255, 0.4);

      &::before {
        left: 100%;
      }
    }

    &:active {
      transform: translateY(-1px);
    }
  }

  .el-checkbox {
    color: #0066ff;
    font-weight: 500;
    
    .el-checkbox__input.is-checked .el-checkbox__inner {
      background-color: #0066ff;
      border-color: #0066ff;
    }
  }

  .link-type {
    color: #0066ff;
    text-decoration: none;
    font-weight: 500;
    transition: all 0.3s ease;
    
    &:hover {
      color: #0099ff;
      text-shadow: 0 0 10px rgba(0, 102, 255, 0.3);
    }
  }
}

@keyframes inputGlow {
  0% {
    box-shadow: 0 0 10px rgba(0, 102, 255, 0.2);
  }
  50% {
    box-shadow: 0 0 20px rgba(0, 102, 255, 0.4);
  }
  100% {
    box-shadow: 0 0 10px rgba(0, 102, 255, 0.2);
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #666;
}

.login-code {
  width: 33%;
  height: 45px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
    border-radius: 8px;
    border: 2px solid rgba(0, 102, 255, 0.3);
    transition: all 0.3s ease;
    
    &:hover {
      border-color: #0066ff;
      box-shadow: 0 0 10px rgba(0, 102, 255, 0.3);
    }
  }
}

.login-code-img {
  height: 45px;
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #0066ff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  text-shadow: 0 1px 2px rgba(0, 102, 255, 0.1);
  z-index: 2;
}

@media screen and (max-width: 768px) {
  .login {
    padding-right: 5%;
  }

  .login-form {
    width: 90%;
    max-width: 400px;
  }

  .gear {
    display: none;
  }
}

@media screen and (max-width: 480px) {
  .login {
    padding-right: 0;
    justify-content: center;
  }

  .login-form {
    width: 95%;
    padding: 30px 25px 15px 25px;
  }

  .title {
    font-size: 28px;
  }
}
</style>
